package engine.videoCapture;

import java.awt.Graphics;
import java.awt.GraphicsConfiguration;
import java.awt.GraphicsDevice;
import java.awt.GraphicsEnvironment;
import java.awt.HeadlessException;
import java.awt.Image;
import java.awt.Transparency;
import java.awt.image.BufferedImage;

import javax.media.Buffer;
import javax.media.control.FrameGrabbingControl;
import javax.media.format.VideoFormat;
import javax.media.util.BufferToImage;
import javax.swing.ImageIcon;

/**
 * GrabFrame Grabs a frame given the FrameGrabbingControl
 * @author Manu
 */
public class GrabFrame {
	
	/* *********************************************************
	 * Attributes
	 * ********************************************************* */
	
	private static BufferedImage _GrabbedBufferedImage = null;
	private static Image _GrabbedImage;
	static Buffer _Buffer = new Buffer();
	static BufferToImage _BufferToImage = null;
	
	/* *********************************************************
	 * Instance Methods
	 * ********************************************************* */
	

	/**
	 * This method grabs a frame from the webcam and returns an image
	 * @param theFrameGrabbingControl the FrameGrabbingControl to grab
	 * frames from the webcam
	 * @return BufferedImage the BufferedImage grabbed
	 */
	public static	BufferedImage doGrabFrame(FrameGrabbingControl theFrameGrabbingControl){
		System.out.println("eg.videoCapture.GrabFrame doGrabFrame start");
		//grab a frame and put it into the buffer
		_Buffer = theFrameGrabbingControl.grabFrame();
		//create an image with the buffered frame
		if (_Buffer.getFormat()!=null) {
			_BufferToImage = new BufferToImage((VideoFormat)_Buffer.getFormat());
			_GrabbedImage = _BufferToImage.createImage(_Buffer);
			//make it a buffered image and this will be the new background
			_GrabbedBufferedImage = toBufferedImage(_GrabbedImage);
		}
		else
		{
			System.out.println("No data in the Image!\n");
		}
		System.out.println("eg.videoCapture.GrabFrame doGrabFrame over!");
		return _GrabbedBufferedImage;
	}
	
	
	
	/**
	 * This function returns a BufferedImage containing the given Image
	 * @param animage an Image class instance
	 * @return BufferedImage a BufferedImage class instance
	 */
	public static BufferedImage toBufferedImage(Image animage) {
		if (animage instanceof BufferedImage) {
			return (BufferedImage)animage;
		}

		// This code ensures that all the pixels in the image are loaded
		animage = new ImageIcon(animage).getImage();

		// Create a buffered image with a format that's compatible with the screen
		BufferedImage bimage = null;
		GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
		try {
			// Create the buffered image
			GraphicsDevice gs = ge.getDefaultScreenDevice();
			GraphicsConfiguration gc = gs.getDefaultConfiguration();
			bimage = gc.createCompatibleImage(
					animage.getWidth(null), animage.getHeight(null), Transparency.OPAQUE);
		} catch (HeadlessException e) {
		}

		if (bimage == null) {
			// Create a buffered image using the default color model
			bimage = new BufferedImage(animage.getWidth(null), animage.getHeight(null), BufferedImage.TYPE_INT_RGB);
		}

		// Copy image to buffered image
		Graphics g = bimage.createGraphics();

		// Paint the image onto the buffered image
		g.drawImage(animage, 0, 0, null);
		g.dispose();

		return bimage;
	}
}
